package review;

import java.util.HashMap;
import java.util.Map;

/**
 * 给出 A = "ABCD" B = "ACD"，返回 true
 *
 * 给出 A = "ABCD" B = "AABC"， 返回 false
 */
public class 一个字符串是否包含另一个字符串的所有字符 {

    public static boolean compareStrings(String a,String b) {
        Map<String,Integer> map = new HashMap<>();
        for (int i = 0; i < a.length(); i++) {
            if(map.containsKey(a.charAt(i) + "")) {
                Integer count = map.get(a.charAt(i) + "");
                map.put(a.charAt(i) + "",++count) ;
            } else {
                map.put(a.charAt(i) + "",1);
            }
        }
        for (int i = 0; i < b.length(); i++) {
            if(!map.containsKey(b.charAt(i) + "")) {
                return false;
            }
            Integer integer = map.get(b.charAt(i) + "");
            if(integer <= 0) {
                return false;
            }
            map.put(b.charAt(i) + "", --integer);
        }
        return true;
    }

    public static void main(String[] args) {
        boolean b = compareStrings("ABCD", "AABC");
        System.out.println(b);

    }
}
